<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>奖项申报</title>
@include('common-css')
<style>
.input_box {
    width: 100%;
    height: 40px;
    margin-top: 15px;
    margin-bottom: 15px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
}

.input_label {
    width: 120px;
    box-sizing: border-box;
}

.application-list {
    margin-top: 20px;
}

.phase-tag {
    color: #1890ff;
    border: 1px solid #91d5ff;
    border-radius: 3px;
    background: #e6f7ff;
    font-size: 12px;
    padding: 2px 3px;
}

.ant-table-tbody > tr > td, .ant-table-thead > tr > th {
    vertical-align: middle;
    line-height: 25px;
}

.recall_reason {
    color: red
}
</style>
</head>
<body>
<div id="app">

</div>
</body>
</html>

<script id="appTemplate" type="x-template">
<a-locale-provider :locale="antdLocale">
    <div class="container">
        @include('home.header')

        <main>
            <div class="common_container">

                @include('home.aside', ['selectedItem' => 'apply'])

                <div class="common_content">
                    <div class="common_box">

{{--                        @if (config('app.award_application_enabled'))--}}
{{--                            <div>--}}
{{--                                <a-button type="primary" @click="showChooseAwardToApplyDialog">我要报奖</a-button>--}}
{{--                            </div>--}}
{{--                        @else--}}
{{--                            <div>申报已截止</div>--}}
{{--                        @endif--}}


                        <div v-if="applicationOpenAwardIds.length > 0">
                            <a-button type="primary" @click="showChooseAwardToApplyDialog">创建报奖项目</a-button>
                        </div>
                        <div v-else>申报已截止</div>

                        <a-table class="application-list"
                                 rowKey="id"
                                 :columns="applicationListColumns"
                                 :dataSource="applications">
                            <span slot="award_name" slot-scope="text, record">
                                @{{ findAwardInfoById(record.award_id).name }}
                            </span>
                            <span slot="project_name" slot-scope="text, record">
                                @{{ record.project_name || '-' }}
                            </span>
                            <span slot="prof_category" slot-scope="text, record">
                                <template v-if="record.award_id == 1">
                                     <a-select style="width: 140px;" size="small"
                                               :disabled="!canProjectSubmitFormalReview(record)"
                                               v-model="record.prof_category"
                                               @change="handleProfCategoryChange(record, $event)">
                                        <a-select-option v-for="category in profCategories"
                                                         :value="category"
                                                         :key="category">@{{ category }}
                                        </a-select-option>
                                    </a-select>
                                </template>
                                <template v-else>无</template>
                            </span>
                            <span slot="nominators" slot-scope="text, record">
                                <template v-if="findAwardInfoById(record.award_id).nomination_required">
                                    <p v-for="nominatorId in record.nominator_ids">@{{ getNominatorById(nominatorId).name }}</p>
                                </template>
                                <template v-else>-</template>
                            </span>
                            <span slot="application_form" slot-scope="text, record">

                                <a :href="'/home/fill-form?id=' + record.id" target="_blank">
                                    <!--申报关闭后 退回修改项目（B1.2）可提交-->
                                    <span v-if="canProjectSubmitFormalReview(record)">填写</span>
                                    <span v-else>查看</span>
                                </a>

                            </span>
                            <span slot="status" slot-scope="text, record">

                                <a-tag color="blue">
                                    <!--@edit-->
                                    <!--@time 2020-06-18-->
{{--                                    <span v-if="['B1', 'B1.2', 'B2'].includes(record.status_info.status)">@{{ record.status_info.status }}</span>--}}
{{--                                    <span v-else>等待评审</span>--}}
                                    <span v-if="['B1', 'B1.2', 'B2', 'E1', 'E2', 'E3', 'E4','I1'].includes(record.status_id)">@{{ record.status_info.status }}</span>
                                    <span v-else>等待评审</span>
                                    <!--@edit-->
                                    <!--@time 2020-06-18-->
                                </a-tag>

                                <div v-if="record.status_id == 'B1.2'" class="recall_reason">原因: @{{ record.status_remarks }}</div>

                            </span>
                            <span slot="todo" slot-scope="text, record">
                                <span class="orange">@{{ record.status_info.applicant_todo }}</span>
                                <br>
                                <a-popover title="邮寄地址" v-if="false">
                                    <template slot="content">
                                        <table>
                                            <tr>
                                                <td width="50">收件人:</td>
                                                <td>王工</td>
                                            </tr>
                                            <tr>
                                                <td>电话:</td>
                                                <td>010-64868878-811</td>
                                            </tr>
                                            <tr>
                                                <td>地址:</td>
                                                <td>北京市朝阳区大屯路科学园南里风林西奥中心B座14层</td>
                                            </tr>
                                            <tr>
                                                <td>邮编:</td>
                                                <td>100101</td>
                                            </tr>
                                        </table>
                                    </template>
                                    <a style="font-size: 12px;" href="">查看邮寄地址</a>
                                </a-popover>
                            </span>
                            <span slot="operations" slot-scope="text, record">

                            <template v-if="canProjectSubmitFormalReview(record)">
                                <a-popconfirm title="确认已经完成申报书的填写，现在提交？"
                                              @confirm="handleSubmitApplicationForm(record)" okText="是"
                                              cancelText="否">
                                    <a-button type="primary" size="small"
                                              :disabled="submittingApplicationForm">提交形式审查</a-button>
                                </a-popconfirm>
                                <a-dropdown v-if="record.status_id == 'B1'" style="margin-left: 10px;"
                                            :trigger="['click']">
                                    <a class="ant-dropdown-link" href="#">
                                        更多<a-icon type="down"/>
                                    </a>
                                    <a-menu slot="overlay">
                                        <a-menu-item v-if="record.status_id == 'B1'" key="delete-application">
                                         <a-popconfirm title="确认删除申报信息？"
                                                       @confirm="handleDeleteProject(record)" okText="是"
                                                       cancelText="否">
                                                <div style="color: red">删除</div>
                                            </a-popconfirm>
                                        </a-menu-item>
                                    </a-menu>
                                </a-dropdown>
                            </template>

                            </span>
                        </a-table>

                    </div>
                </div>
            </div>
        </main>

        <a-modal :width="500" v-model="chooseAwardToApplyDialogVisible" title="请选择要申报的奖项"
                 @ok="handleChooseAwardToApplyOk">

            <a-radio-group v-model="newApplicationAwardId">
                <template v-for="awardInfo in awardsInfo">
                    <template v-if="applicationOpenAwardIds.includes(awardInfo.id)">
                        <a-radio style="display: block; line-height: 30px;" :value="awardInfo.id">
                            <b>@{{awardInfo.name}}</b>
                        </a-radio>
                        <p style="color: #666; font-size: 12px; margin-left: 24px; margin-bottom: 10px;">
                            @{{awardInfo.desc}}</p>
                    </template>
                </template>
            </a-radio-group>

        </a-modal>

        <a-modal :width="500" v-model="chooseNominatorsDialogVisible"
                 title="{{ config('vars.strings.choose_nominators') }}"
                 @ok="handleChooseNominatorsOk">

            <p style="margin-bottom: 10px;">@{{ newApplicationAwardInfo.nomination_rule_desc }}</p>

            @foreach ([1, 2, 3, 4, 5] as $i)

                <div v-if="newApplicationAwardInfo.num_of_nominator_selections >= {{ $i }}"
                     style="margin-top: 10px;">
                    {{ config('vars.strings.nominator') }}
                    @if ($i == 1)
                        <span v-if="newApplicationAwardInfo.num_of_nominator_selections > 1">1</span>
                    @else
                        <span>{{ $i }}</span>
                    @endif

                    <select style="margin-left: 5px; width: 300px; padding: 3px 5px; background: #fff;"
                            v-model="newApplicationNominatorId{{ $i }}">
                        <option :value="0">请选择</option>
                        @if (config('vars.nominator_group_enabled'))
                            <optgroup v-for="nominatorGroup in nominatorGroupNames" :label="nominatorGroup"
                                      :key="nominatorGroup">
                                <option :value="nominator.id"
                                        v-for="nominator in getNominatorsByGroup(nominatorGroup)"
                                        :key="nominator.id">@{{ nominator.name }}
                                </option>
                            </optgroup>
                        @else
                            <option :value="nominator.id"
                                    v-for="nominator in nominators"
                                    :key="nominator.id">@{{ nominator.name }}
                            </option>
                        @endif
                    </select>

                </div>

            @endforeach

            <p style="font-size: 12px; margin-top: 10px;" class="orange">
                注：<br/>
                1. {{ config('vars.strings.nominator') }}不支持修改，请与相关单位确认后再选择
                <br/>
                2. 如果您的{{ config('vars.strings.nominator') }}不在列表里，请与{{ config('vars.award_office_name') }}
                联系 {{ config('vars.award_office_tel') }}
            </p>

        </a-modal>

    </div>
</a-locale-provider>
</script>

@include('common-js')

<script>
const applicationListColumns = [{
    width: 40,
    title: '序号',
    dataIndex: 'num'
}, {
    width: 80,
    title: '奖项',
    dataIndex: 'award_name',
    scopedSlots: {
        customRender: 'award_name'
    }
}, {
    width: 80,
    title: '项目名称',
    dataIndex: 'project_name',
    scopedSlots: {
        customRender: 'project_name'
    }
},
{
    width: 100,
    title: '专业类别',
    dataIndex: 'prof_category',
    scopedSlots: {
        customRender: 'prof_category'
    }
},{
    width: 80,
    title: '申报书',
    dataIndex: 'application_form',
    scopedSlots: {
        customRender: 'application_form'
    }
}, {
    width: 100,
    title: '申报状态',
    dataIndex: 'status',
    scopedSlots: {
        customRender: 'status'
    }
}, {
    width: 100,
    title: '待办事项',
    dataIndex: 'applicant_todo',
    scopedSlots: {
        customRender: 'todo'
    }
}, {
    width: 80,
    title: '操作',
    key: 'operations',
    scopedSlots: {
        customRender: 'operations'
    }
}];

var awardsInfo = {!! json_encode(array_values(config('vars.awards')), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};
var nominators = {!! json_encode($nominators, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};
var applications = {!! json_encode($applications, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) !!};
console.log(applications);
var applicationOpenAwardIds = {!! json_encode($applicationOpenAwardIds) !!};

function processApplicationsData(applications) {
    var n = 1
    applications.forEach(item => {
        item.num = n
        n += 1
    })
}

processApplicationsData(applications)

Vue.prototype.$axios = axios;
var app = new Vue({
    el: '#app',
    template: '#appTemplate',
    data: {
        profCategories,
        //是否开启申报
        DeclarePhaseStatus: {{$DeclarePhaseStatus ? 'true' : 'false'}},
        //是否结束形式审查
        AtPhaseFormalReview:{{$AtPhaseFormalReview ? 'true' : 'false'}},
        applicationOpenAwardIds,
        chooseAwardToApplyDialogVisible: false,
        chooseNominatorsDialogVisible: false,
        newApplicationAwardId: applicationOpenAwardIds.length ? applicationOpenAwardIds[0] : null,
        newApplicationNominatorId1: 0,
        newApplicationNominatorId2: 0,
        newApplicationNominatorId3: 0,
        newApplicationNominatorId4: 0,
        newApplicationNominatorId5: 0,
        applications: applications,
        nominators: nominators,
        submittingApplicationForm: false,
        deletingApplicationForm: false,
        awardsInfo: awardsInfo,
        applicationListColumns: applicationListColumns,
        changePasswordDialogVisible: false,
        passwordInfo: {
            password: '',
            passwordConfirm: ''
        },
        antdLocale: antd.locales.zh_CN
    },
    computed: {
        newApplicationAwardInfo() {
            return this.findAwardInfoById(this.newApplicationAwardId)
        },
        nominatorGroupNames() {
            var result = []
            this.nominators.forEach(item => {
                if (!result.includes(item.group_name)) {
                    result.push(item.group_name)
                }
            })
            return result
        }
    },
    mounted() {
    },
    methods: {
        changeApplicationStatus(application, statusId) {
            console.log('application', application)
            this.submittingApplicationForm = true
            antd.message.loading('正在处理，请稍候', 0)
            axios.post('/api/award/change-application-status', {
                application_id: application.id,
                status_id: statusId
            }).then(res => {
                antd.message.destroy()
                var data = res.data
                if (data.ok) {
                    antd.message.success('操作成功')
                    this.reloadData()
                } else {
                    antd.message.warn(data.message, 3)
                }
                this.submittingApplicationForm = false
            })
        },
        downloadAwardDoc(path) {
            window.open(path)
        },
        printAward(id) {
            window.open('/apply/show-print?award_id=' + id)
        },
        getNominatorById(id) {
            return this.nominators.filter(item => {
                return item.id == id
            })[0]
        },
        getNominatorsByGroup(nominatorGroup) {
            return this.nominators.filter(item => {
                return item.group_name == nominatorGroup
            })
        },
        handleSubmitApplicationForm(application) {
            axios.get('/api/common/get-workflow-phases').then(res => {
                // debugger
                let declare = res.data.phases[0]
                let declare_tow = res.data.phases[1]
                var applicationPhaseEnded = application.award_id == 1 && (declare.has_begun != 1 || declare.has_ended != 0);
                var applicationPhaseEndedTwo = application.award_id == 1 && (declare_tow.has_begun != 1 || declare_tow.has_ended != 0);

                if (applicationPhaseEnded && application.status_id !== 'B1.2') {
                    warn('申报已截止,无法执行该操作')
                    setTimeout(function () {
                        window.location.reload()
                    },1200)
                }else if (applicationPhaseEndedTwo && application.status_id == 'B1.2') {
                    warn('形式审查阶段已结束，不能提交项目')
                    setTimeout(function () {
                        window.location.reload()
                    },2200)
                } else {

                    console.log('application', application)
                    this.submittingApplicationForm = true
                    antd.message.loading('正在处理，请稍候', 0)
                    axios.post('/api/award/applicant-submit-application-form', {
                        application_id: application.id
                    }).then(res => {
                        antd.message.destroy()
                        console.log(res)
                        var data = res.data
                        if (data.ok) {
                            antd.message.success('提交成功')
                            this.reloadData()
                        } else {
                            antd.message.warn(data.message, 3)
                        }
                        this.submittingApplicationForm = false
                    })

                }
            })
        },
        handleDeleteProject(project) {
            console.log('project', project)
            this.deletingApplicationForm = true
            antd.message.loading('正在处理，请稍候', 0)
            axios.post('/api/award/delete-application', {
                application_id: project.id
            }).then(res => {
                antd.message.destroy()
                console.log(res)
                var data = res.data
                if (data.ok) {
                    antd.message.success('删除成功')
                    this.reloadData()
                } else {
                    antd.message.warn(data.message, 3)
                }
                this.deletingApplicationForm = false
            })
        },
        showChooseNominatorsDialog() {
            this.chooseNominatorsDialogVisible = true
        },
        handleChooseNominatorsOk() {
            console.log('newApplicationAwardId', this.newApplicationAwardId)
            var nominatorIds = [this.newApplicationNominatorId1, this.newApplicationNominatorId2, this.newApplicationNominatorId3, this.newApplicationNominatorId4, this.newApplicationNominatorId5]
            nominatorIds = nominatorIds.filter(item => item != 0)
            var awardId = this.newApplicationAwardId
            var awardInfo = this.newApplicationAwardInfo
            console.log('nominatorIds', nominatorIds)
            if (nominatorIds.length == 0) {
                antd.message.warn('{{ config('vars.strings.no_nominator_selected') }}')
                return
            }
            this.submitNewApplication(awardId, nominatorIds)
            this.chooseNominatorsDialogVisible = false
        },
        handleChooseAwardToApplyOk() {
            console.log('newApplicationAwardId', this.newApplicationAwardId)
            this.chooseAwardToApplyDialogVisible = false
            var awardId = this.newApplicationAwardId
            var awardInfo = this.newApplicationAwardInfo
            if (awardInfo.nomination_required) {
                this.showChooseNominatorsDialog()
            } else {
                this.submitNewApplication(awardId, [])
            }
        },
        submitNewApplication(awardId, nominatorIds) {
            antd.message.loading('正在处理，请稍候', 0)
            axios.post('/api/award/create-application', {
                award_id: awardId,
                nominator_ids: nominatorIds
            }).then(res => {
                console.log(res)
                var data = res.data
                if (data.ok) {
                    antd.message.success('创建成功，请在线填写申报书')
                    this.reloadData()
                } else {
                    antd.message.destroy()
                    antd.message.warn(data.message, 3)
                }
            })
        },
        showChooseAwardToApplyDialog() {
            this.chooseAwardToApplyDialogVisible = true
            // axios.get('/api/common/get-workflow-phases').then(res => {
            //     let declare = res.data.phases[0]
            //     if (declare.has_begun != 1 || declare.has_ended != 0) {
            //         warn('申报已截止,无法执行该操作')
            //         setTimeout(function () {
            //             window.location.reload()
            //         },1200)
            //     } else {
            //     }
            //
            // })
        },
        canProjectSubmitFormalReview(project) {
            var status_review=this.AtPhaseFormalReview
            // console.log(status_review);
            // debugger
            var status=this.applicationOpenAwardIds.includes(project.award_id);
            if (status && (project.status_id == 'B1' || project.status_id == 'B1.2')) {
                return true
            } else if (project.status_id == 'B1.2') {
                return true
            }
            return false
        },
        findAwardInfoById(id) {
            return this.awardsInfo.filter(item => {
                return item.id == id
            })[0]
        },
        reloadData() {
            axios.post('/api/award/get-applications')
                .then(res => {
                    antd.message.destroy()
                    var applications = res.data
                    console.log(applications)
                    processApplicationsData(applications)
                    this.applications = applications
                })
        },
        handleHeaderDropdownItemClick: handleHeaderDropdownItemClick,
        handleChangePasswordOk: handleChangePassword,
        handleOrgBusinessLicensePhotoBeforeUpload(file) {
            console.log('handleOrgBusinessLicensePhotoBeforeUpload', file)
            var filename = file.name.toLowerCase()
            if (filename.endsWith('.jpg') || filename.endsWith('.jpeg') || filename.endsWith('.png')) {
                antd.message.loading('正在上传...', 0)
                return true
            } else {
                antd.message.warn('不允许上传该文件格式')
                return false
            }
        },
        handleOrgBusinessLicensePhotoUploadChange(event) {
            console.log('handleOrgBusinessLicensePhotoUploadChange', event)
            if (event.file.status == 'done') {
                antd.message.destroy()
                if (event.file.response.ok) {
                    let photo_path = event.file.response.url
                    axios.post('/api/auth/reupload-org-bizlic-Photo', {photo_path})
                        .then(res => {
                            antd.message.success('上传成功')
                            history.go(0)
                        })
                } else {
                    antd.message.warn('上传失败')
                }
            }
        },
        handleProfCategoryChange(record, value) {
            axios.post('/api/apply/update-project-attrs', {
                project_id: record.id,
                attrs: {
                    prof_category: value
                }
            }).then(response => {
               if (response.data.ok) {
                   success('修改成功')
               } else {
                   error('修改失败，请重试')
               }
            })
        },
    }
})
</script>
